package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.bean.response.HealthEducationList;
import com.bsoft.gol.hcs.specialist.entity.FusIndexResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Description: 指标结果dao
 * @Author: zhaorb
 * @Date: 2020/7/20 14:56
 **/
public abstract class FusIndexResultDAO extends HibernateSupportDelegateDAO<FusIndexResult> {


    @DAOMethod(sql = " from FusIndexResult where indexId =:indexId order by age_limit_union asc ")
    public abstract List<FusIndexResult> queryFusIndexResultList(@DAOParam("indexId") String indexId);


    @DAOMethod(sql = " from FusIndexResult where indexId =:indexId and gender=:sex and ageUpperLimit>:age and ageLowerLimit<:age")
    public abstract List<FusIndexResult> queryFusIndexResultListByAge(@DAOParam("indexId") String indexId,@DAOParam("age") Integer age,@DAOParam("sex") String sex);

    @DAOMethod(sql = " from FusIndexResult where indexResultId =:indexResultId ")
    public abstract FusIndexResult getFusIndexResultByKey(@DAOParam("indexResultId")String indexResultId);

    @DAOMethod(sql = "delete from FusIndexResult where indexId =:indexId")
    public abstract void deleteFusIndexResultIndexId(@DAOParam("indexId") String indexId);



    /**
     * @description: 根据病种查询健康监测指标列表
     * @author: chenJij@bsoft.com.cn
     * @param: mpiId
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> queryHpiCodeList(final String mpiId) {

        HibernateStatelessResultAction<List<Map<String,Object>>> action =
                new AbstractHibernateStatelessResultAction<List<Map<String,Object>>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer sql = new StringBuffer();
                        sql.append("SELECT DISTINCT " +
                                " c.monitor_id AS itemCode  " +
                                "FROM " +
                                " fus_team_chronic_group_record a, " +
                                " follow_up_route b, " +
                                " follow_up_route_detail_form c " +
                                "WHERE " +
                                " a.chronic_disease_id = b.disease_type_id " +
                                "AND b.route_id = c.route_id " +
                                "AND a.person_id =:mpiId");
                        Query query = ss.createSQLQuery(sql.toString());
                        query.setParameter("mpiId", mpiId);
                        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<HealthEducationList> followUpPlanList = new ArrayList<HealthEducationList>();
                        List<Map<String,Object>> list = query.list();
                        if (list.size() > 0) {
                            setResult(list);
                        } else {
                            setResult(null);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    /**
     * @description: 根据病种查询健康监测指标列表  -----手动随访添加的健康监测
     * @author: chenJij@bsoft.com.cn
     * @param: mpiId
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> queryHpiCodeListByArtificial(final String mpiId) {

        HibernateStatelessResultAction<List<Map<String,Object>>> action =
                new AbstractHibernateStatelessResultAction<List<Map<String,Object>>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer sql = new StringBuffer();
                        sql.append("SELECT DISTINCT b.monitor_id AS itemCode  " +
                                "FROM fus_team_chronic_group_record a,follow_up_records_extend b WHERE " +
                                " a.team_chronic_group_id = b.team_chronic_group_id " +
                                "AND a.person_id =:mpiId");
                        Query query = ss.createSQLQuery(sql.toString());
                        query.setParameter("mpiId", mpiId);
                        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<HealthEducationList> followUpPlanList = new ArrayList<HealthEducationList>();
                        List<Map<String,Object>> list = query.list();
                        if (list.size() > 0) {
                            setResult(list);
                        } else {
                            setResult(null);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
